﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using RibbonEditor.AppCode;

namespace RibbonEditor.Templates
{
    public class ControlRef : ITemplate, IXmlBuildable
    {
        public Enumerations.DisplayMode? DisplayMode { get; set; }
        public string TemplateAlias { get; set; }

        public ControlRef()
        {}

        public ControlRef(XmlNode node)
        {
            try
            {
                this.DisplayMode = (Enumerations.DisplayMode)Enum.Parse(typeof(Enumerations.DisplayMode), XmlHelper.GetAttribute(node, "DisplayMode"));
                this.TemplateAlias = XmlHelper.GetAttribute(node, "TemplateAlias");
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void AddNode(XmlNode rowNode)
        {
            try
            {
                XmlNode controlRefNode = XmlHelper.AddNode(rowNode, "ControlRef");
                XmlHelper.AddAttribute(controlRefNode, "DisplayMode", this.DisplayMode);
                XmlHelper.AddAttribute(controlRefNode, "TemplateAlias", this.TemplateAlias);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void BuildTreeViewElement(TreeNode parentNode)
        {
            try
            {
                TreeNode node = new TreeNode("ControlRef");
                node.ContextMenuStrip = ContextMenuStripHelper.BuildStandardContextMenuStrip(node);
                node.Tag = this;

                parentNode.Nodes.Add(node);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
